package com.xbongbong.paas.script.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.SyncUserToContactDTO;
import com.xbongbong.saas.model.DayHealthModel;
import com.xbongbong.saas.service.SyncDataToXbbService;
import com.xbongbong.sys.model.CompanyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

/***
 *　同步数据到逍邦公司
 *
 * @author hongxiao
 * @date 2021-10-31 16:54
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/sync")
public class SyncDataToXbbController {

    private static final Logger LOG = LoggerFactory.getLogger(SyncDataToXbbController.class);


    @Autowired
    protected CompanyModel companyModel;
    @Autowired
    protected DayHealthModel dayHealthModel;
    @Resource
    private SyncDataToXbbService syncDataToXbbService;


    /***
     * 缓存联系人数据
     *
     * @param syncUserToContactDTO 入参
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-10-31 17:49
     * @since
     * @version
     */
    @RequestMapping(value = "/add/contact/redis")
    public String addContactRedis(@RequestBody @Valid SyncUserToContactDTO syncUserToContactDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = syncDataToXbbService.addContactRedis(syncUserToContactDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);

    }

    /***
     *　联系同步脚本，每5分钟执行一次
     *
     * @param request
     * @param: response
     * @param: modelMap
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-10-31 21:04
     * @since
     * @version
     */
    @RequestMapping(value = "/syncContactToPro")
    public String syncContactToPro(HttpServletRequest request,
                                  HttpServletResponse response, Map<String, Object> modelMap) {
        JSONObject returnMsg = new JSONObject();
        returnMsg.put("code", 1);
        try {
            String key = request.getParameter("key");
            String msg = syncDataToXbbService.syncContactToXbb(key);
            returnMsg.put("msg", msg);
            return returnMsg.toJSONString();
        } catch (Exception e) {
            LOG.error("syncContactToPro错误！");
            returnMsg.put("msg", "发生异常");
            return returnMsg.toJSONString();
        }
    }

}
